<?php

/**
 * 常用公共函数
 */
class common {

    private static $public_key = '';
    private static $private_key = '';

    function __construct() {
        //公钥
        self::$public_key = file_get_contents(str_replace('\\', "/", APP_ROOT) . "/config/rsakey/rsa_public_key.pem");
        //私钥
        self::$private_key = file_get_contents(str_replace('\\', "/", APP_ROOT) . "/config/rsakey/rsa_private_key.pem");
    }

    //生成加密参数
    final function pbulic_encode($uid) {
        $data['uid'] = $uid;
        $data['encode_key'] = 'nigelaji';
        $data_encode = json_encode($data);
        //公钥加密
        $encrypted = '';
        openssl_public_encrypt($data_encode, $encrypted, self::$public_key);
        return base64_encode($encrypted);
    }

    //解密参数
    final function public_decode($encrypted) {
        $decrypted = '';
        openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::$private_key); //私钥解密
        return json_decode($decrypted, TRUE);
    }

}
